package com.shop.web.admin;

import com.shop.annotation.MyLog;
import com.shop.dao.OrderDao;
import com.shop.entity.Order;
import com.shop.entity.OrderItem;
import com.shop.entity.User;
import com.shop.entity.UserGroup;
import com.shop.entity.pojo.ResultBean;
import com.shop.service.UserGroupService;
import com.shop.service.OrderService;
import com.shop.service.UserService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin/usergroup")
public class AdminUserGroupController {
    @Autowired
    private UserGroupService userGroupService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;
    /**
     * 打开用户列表页面
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "admin/usergroup/list";
    }

    /**
     * 获取所有用户列表
     *
     * @param pageindex
     * @return
     */
    @ResponseBody
    @RequestMapping("/list.do")
    public ResultBean<List<UserGroup>> findAllUsergroup(int pageindex,
                                                    @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        Pageable pageable = new PageRequest(pageindex, pageSize, null);
        List<UserGroup> userGroups = userGroupService.findAll(pageable).getContent();
        return new ResultBean<>(userGroups);
    }

    @ResponseBody
    @RequestMapping("/getTotal.do")
    public ResultBean<Integer> geTotal() {
        Pageable pageable = new PageRequest(1, 15, null);
        int total = (int) userGroupService.findAll(pageable).getTotalElements();
        return new ResultBean<>(total);
    }

    /**
     * 分析所有用户平均消费水平
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/analyse.do")
    @MyLog(title = "管理员模块", content = "分析用户信息")
    public ResultBean<Boolean> analyse(){
        List<User> users = userService.findAll_1();
        List<Order> orders = orderService.findAll_1();
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        for(int i = 0;i<users.size();i++){
            map.put(users.get(i).getId(),new ArrayList<>());
        }
        for(int i = 0;i<orders.size();i++){
            int userid = orders.get(i).getUserId();
            int id=orders.get(i).getId();
            map.get(userid).add(id);
        }
        for(int key: map.keySet()){
            List<Integer> tmp = map.getOrDefault(key, null);
            double total=0;
            double count=0;
            for (int k=0;k<tmp.size();k++){
                List<OrderItem> orderItem=orderService.findItems(tmp.get(k));
                for (int h=0;h<orderItem.size();h++){
                    total=total+orderItem.get(h).getSubTotal();
                    count=count+orderItem.get(h).getCount();
                }
            }
            double averagePrice=0;
            if(count!=0){
                averagePrice=total/count;
            }
            UserGroup userGroup=new UserGroup();
            userGroup.setId(key);
            userGroup.setUsername(users.get(key-1).getUsername());
            userGroup.setAveragePrice(averagePrice);
            userGroupService.update(userGroup);

        }
        return new ResultBean<>(true);
    }
}
